<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('ROOT')) die();

if (isset($_GET['manage'])) {
	$index = 'modules';

	$table = Html::tr(Html::td(__('Назва'), 'chead', 'left', '9%') . Html::td(__('Версія'), 'chead', 'left', '2%') . Html::td(__('Автор'), 'chead', 'left', '9%') . Html::td(__('Опис'), 'chead', 'left', '60%') . Html::td(__('Статус'), 'chead', 'left', '10%') . Html::td(__('Дія'), 'chead', 'left', '10%'));
	foreach ($ains[$index] as $addn) {
		if ($addn == 'users' or $addn == 'captcha') {
			$disable = '';
		} else {
			$disable = Html::tag(__('Вимкнути'), 'a', array('onclick' => 'return confirm(\''.__('Ви впевнені що хочете вимкнути модуль? Всі дані буде втрачено!').'\');','href' => LINK_ROOT . 'adminarea/?do=mods&amp;deactivate=' . $index . '&amp;name=' . $addn));
		}
		if (is_file(MAINDIR . '/modules/' . $addn . '/info.xml')) {
			$nfo = simplexml_load_file(MAINDIR . '/modules/' . $addn . '/info.xml');
		}
		$name = $addn;
		if (isset($nfo)) {
			$name = $nfo->name;
			if ($nfo->module_url) {
				$name = Html::tag($name, 'a', array('href' => $nfo->module_url));
			}
		}
		$author = '';
		if (isset($nfo)) {
			$author = $nfo->author;
			if ($nfo->author_url) {
				$author = Html::tag($author, 'a', array('href' => $nfo->author_url));
			}
		}

		$table .= 
		Html::tr(Html::tag($name, 'td', array('class' => 'confclass')) . 
		Html::tag(isset($nfo) ? $nfo->version : '', 'td', array('class' => 'confclass')) . 
		Html::tag($author, 'td', array('class' => 'confclass')) . 
		Html::tag(isset($nfo) ? $nfo->description : '', 'td', array('class' => 'confclass')) . 
		Html::tag(Html::tag(__('Модуль увімкнений'), 'span', array('style' => 'color:green')), 'td', array('class' => 'confclass')) . 
		Html::td($disable, 'confclass', 'left', '33%'));
		unset($nfo);
	}
	foreach ($aunins[$index] as $addn) {
		if (is_file(MAINDIR . '/modules/' . $addn . '/info.xml')) {
			$nfo = simplexml_load_file(MAINDIR . '/modules/' . $addn . '/info.xml');
		}
		$name = $addn;
		if (isset($nfo)) {
			$name = $nfo->name;
			if ($nfo->module_url) {
				$name = Html::tag($name, 'a', array('href' => $nfo->module_url));
			}
		}
		$author = '';
		if (isset($nfo)) {
			$author = $nfo->author;
			if ($nfo->author_url) {
				$author = Html::tag($author, 'a', array('href' => $nfo->author_url));
			}
		}
		$table .= Html::tr(Html::tag($name, 'td', array('class' => 'confclass')) . Html::tag(isset($nfo) ? $nfo->version : '', 'td', array('class' => 'confclass')) . Html::tag($author, 'td', array('class' => 'confclass')) . Html::tag(isset($nfo) ? $nfo->description : '', 'td', array('class' => 'confclass')) . Html::tag(Html::tag(__('Модуль вимкнено'), 'span', array('style' => 'color:red')), 'td', array('class' => 'confclass')) . Html::td(Html::tag(__('Увімкнути'), 'a', array('href' => LINK_ROOT . 'adminarea/?do=mods&amp;activate=' . $index . '&amp;name=' . $addn)), 'confclass', 'left', '33%'));
		unset($nfo);
	}
	Aurus::getInstance()->addContent(Html::table($table));

} elseif (isset($_GET['activate'])) {
	$type = substr($_GET['activate'], 0, -1);

	$module = 'Module_' . $_GET['name'];
	Factory::get($module)->onActivate();

	Mysql::getInstance()->insert('modules', array('type' => $type, 'name' => $_GET['name']));
	Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Додаток увімкнено')));
	Cache::clear();
} elseif (isset($_GET['deactivate'])) {
	$module = 'Module_' . $_GET['name'];
	Factory::get($module)->onDeactivate();

	$type = substr($_GET['deactivate'], 0, -1);
	Mysql::getInstance()->delete('modules', "type='{$type}' AND name='{$_GET['name']}'");
	Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Додаток вимкнено')));
	Cache::clear();
}